package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Company;

public class CompanyLogic extends AbstractBL {
	public Company AddCompany(long hotelId,String name,String address,String address2,String country,
			String state,String city,String zipcode,String phone,String fax,String logo,String photo,
			String website,String vatId,String description,String bName,String bAddress1,String bAddress2,
			String country2,String state2,String bCity,String bZipcode,String bPhone,String bFax,
			String cSal,String cName,String cLastname,String cDesign,String cPhone,String cExtn,
			String cFax,String cEmail,String cMobile) throws EntityNotFoundException{
		Company a = new Company();
		a.setHotelId(hotelId);
		a.setAddress(address);
		a.setAddress2(address2);
		a.setBilladdress(bAddress1);
		a.setBilladdress2(bAddress2);
		a.setBillcity(bCity);
		a.setBillcountry(country2);
		a.setBillfax(bFax);
		a.setBillname(bName);
		a.setBillphone(bPhone);
		a.setBillstate(state2);
		a.setBillzipcode(bZipcode);
		a.setCity(city);
		a.setContactdesignation(cDesign);
		a.setContactemail(cEmail);
		a.setContactextn(cExtn);
		a.setContactfax(cFax);
		a.setContactfirstname(cName);
		a.setContactlastname(cLastname);
		a.setContactmobile(cMobile);
		a.setContactphone(cPhone);
		a.setContactsalutation(cSal);
		a.setCountry(country);
		a.setDescrition(description);
		a.setFax(fax);
		a.setLogo(logo);
		a.setName(name);
		a.setPhone(phone);
		a.setPhoto(photo);
		a.setState(state);
		a.setVatid(vatId);
		a.setWebsite(website);
		a.setZipcode(zipcode);
		
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public Company EditCompany(long id,String name,String address,String address2,String country,
			String state,String city,String zipcode,String phone,String fax,String logo,String photo,
			String website,String vatId,String description,String bName,String bAddress1,String bAddress2,
			String country2,String state2,String bCity,String bZipcode,String bPhone,String bFax,
			String cSal,String cName,String cLastname,String cDesign,String cPhone,String cExtn,
			String cFax,String cEmail,String cMobile) throws EntityNotFoundException{	
		Company a = GetCompany(id);
		a.setAddress(address);
		a.setAddress2(address2);
		a.setBilladdress(bAddress1);
		a.setBilladdress2(bAddress2);
		a.setBillcity(bCity);
		a.setBillcountry(country2);
		a.setBillfax(bFax);
		a.setBillname(bName);
		a.setBillphone(bPhone);
		a.setBillstate(state2);
		a.setBillzipcode(bZipcode);
		a.setCity(city);
		a.setContactdesignation(cDesign);
		a.setContactemail(cEmail);
		a.setContactextn(cExtn);
		a.setContactfax(cFax);
		a.setContactfirstname(cName);
		a.setContactlastname(cLastname);
		a.setContactmobile(cMobile);
		a.setContactphone(cPhone);
		a.setContactsalutation(cSal);
		a.setCountry(country);
		a.setDescrition(description);
		a.setFax(fax);
		a.setLogo(logo);
		a.setName(name);
		a.setPhone(phone);
		a.setPhoto(photo);
		a.setState(state);
		a.setVatid(vatId);
		a.setWebsite(website);
		a.setZipcode(zipcode);
		
		Key	k=datastore.put(a.getEntity());
		Entity e = datastore.get(k);
		a = new Company();
		a.setEntity(e);

		return a;
	}
	
	public Company GetCompany(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Company, id);
		try {
			Entity lang = datastore.get(key);
			Company a = new Company();
			a.setEntity(lang);
			return a;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public Company GetCompanyByHotelId(long hotelId)
	{
		Query q = new Query(EntityName.Company);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);		
		
		q.setFilter(filterHotel);
		
		return GetCompany(datastore.prepare(q).asSingleEntity());
	}
	
	public Company GetCompany(Entity entity) {
		if(entity != null){
			Company item = new Company();
			item.setEntity(entity);
			return item;
		}else{
			return null;
		}
		
	}
	
	public List<Company> GetList(List<Entity> entities)
	{
		List<Company> list = new ArrayList<Company>();
		for(Entity e : entities)
		{
			Company a = new Company();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
